/*
@Time : 2022/5/28 22:38
@Author : gaozhichang
@File : server.go
@des: GoLand
*/
package main

import (
	"context"
	"fmt"
	hello "github.com/gaozhichang/golang-study/grpcdemo/proto"
	"google.golang.org/grpc"
	"net"
)

type myRpcServer struct{}

// 定义rpc服务的方法
func (s *myRpcServer) SayHello(ctx context.Context, in *hello.HelloRequest) (*hello.HelloResponse, error) {
	fmt.Println(in.Name)
	return &hello.HelloResponse{
		Message: "hello: " + in.Name,
	}, nil
}

func main() {
	// 新建tcp监听通道
	listener, err := net.Listen("tcp", ":8062")
	if err != nil {
		panic(err)
	}

	// 实例化gRPC server
	server := grpc.NewServer()
	// 注册 server
	hello.RegisterHelloServer(server, &myRpcServer{})

	fmt.Println("Listen on localhost:8062")
	// 启动监听
	server.Serve(listener)
}
